<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    const obj = {
            uname: 'ikun',
            hobby: ['sing', 'dance', 'rap'],
            age: '2.5',
            fans: {
                uname: '小黑子',
                active: '露出了鸡脚'
            }
        }
        let newObj = {}
        // 浅拷贝  只拷贝第一层
      // 深拷贝
      //   1.let newObj = JSON.parse(JSON.stringify(obj))  //  不能对函数和undifnk进行拷贝
      // 递归函数
      function deepClone(newObj,oldObj) {
        for(let key in oldObj) {
          if (oldObj[key] instanceof Array) {
            newObj[key] = []
            deepClone(newObj[key],oldObj[key])
          } else if(oldObj[key] instanceof Object) {
            newObj[key] = {}
            deepClone(newObj[key],oldObj[key])
          } else {
            newObj[key] = oldObj[key]
          }
        }
      }
      deepClone(newObj, obj)
      newObj.fans.uname = '背带裤'
      console.log(newObj)
      console.log(obj);
  </script>
</body>
</html>